<?php

include_once dirname(dirname(dirname(__FILE__))) . '/shared/db_connection.php';

function getJournal($journal_id) {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetJournalsById(" . $journal_id . ")")
            or die("Query fail: " . mysqli_error());
    $row = mysqli_fetch_array($result);
    $item = array();
    $item["JournalID"] = $journal_id;
    $item["Title"] = $row['Title'];
    $item["Thumb"] = $row['Thumb'];
    $item["ShortDescription"] = $row['ShortDescription'];
    $item["PrintISSN"] = $row['PrintISSN'];
    $item["OnlineISSN"] = $row['OnlineISSN'];
    $item["Publish"] = $row['Publish'];
    $item["Photo"] = $row['Photo'];
    $item["CreationDate"] = $row['CreationDate'];

    close_connect($con);
    return $item;
}

function getAllCategoriesByJournalID($journal_id) {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetAllCategoriesByJournalID(" . $journal_id . ")")
            or die("Query fail: " . mysqli_error());

    $list = array();
    $count = 0;
    while ($row = mysqli_fetch_array($result)) {
        $item = array();
        $item["CategoryID"] = $row['CategoryID'];
        $item["Title"] = $row['Title'];
        $item["Description"] = $row['Description'];
        $item["JournalID"] = $row['JournalID'];
        $item["JournalTitle"] = $row['JournalTitle'];
        $list[$count] = $item;
        $count = $count + 1;
    }

    close_connect($con);
    return $list;
}

function getAllJournalSubDetByJournalID($journal_id) {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetAllJournalSubDetByJournalID(" . $journal_id . ")")
            or die("Query fail: " . mysqli_error());

    $row = mysqli_fetch_array($result);
    $item = array();
    $item["JournalsSubID"] = $row['JournalsSubID'];
    $item["Description"] = $row['Description'];
    $item["JournalID"] = $row['JournalID'];
    $item["CategoryID"] = $row['CategoryID'];
    $item["Title"] = $row['Title'];
    $item["catTitle"] = $row['catTitle'];

    close_connect($con);
    return $item;
}

function getAllJournalSubDetByCatID($category_id) {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetAllJournalSubDetByCatID(" . $category_id . ")")
            or die("Query fail: " . mysqli_error());

    $row = mysqli_fetch_array($result);
    $item = array();
    $item["JournalsSubID"] = $row['JournalsSubID'];
    $item["Description"] = $row['Description'];
    $item["JournalID"] = $row['JournalID'];
    $item["CategoryID"] = $row['CategoryID'];
    $item["Title"] = $row['Title'];
    $item["catTitle"] = $row['catTitle'];

    close_connect($con);
    return $item;
}

function getCategoryById($category_id) {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetCategoryById(" . $category_id . ")")
            or die("Query fail: " . mysqli_error());

    $row = mysqli_fetch_array($result);
    $item = array();
    $item["CategoryID"] = $row['CategoryID'];
    $item["Title"] = $row['Title'];
    $item["Description"] = $row['Description'];
    $item["JournalID"] = $row['JournalID'];
    $item["JournalTitle"] = $row['JournalTitle'];

    close_connect($con);
    return $item;
}

//get total journals
function getAllJournals() {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetAllJournals()")
            or die("Query fail: " . mysqli_error());

    $list = array();
    $count = 0;
    while ($row = mysqli_fetch_array($result)) {
        $item = array();
        $item["JournalID"] = $row['JournalID'];
        $item["Title"] = $row['Title'];
        $item["Thumb"] = $row['Thumb'];
        $item["ShortDescription"] = $row['ShortDescription'];
        $item["PrintISSN"] = $row['PrintISSN'];
        $item["OnlineISSN"] = $row['OnlineISSN'];
        $item["Publish"] = $row['Publish'];
        $item["Photo"] = $row['Photo'];
        $item["CreationDate"] = $row['CreationDate'];
        $item["ImpactFactor"] = $row['ImpactFactor'];
        $list[$count] = $item;
        $count = $count + 1;
    }

    close_connect($con);
    return $list;
}

//get total page
function get_total_page_journals_dal($page_size) {
    $total_journals = getAllJournals();
    $total_page = (count($total_journals) / $page_size) + 1;
    return round($total_page);
}

//get list journals of current page
function get_current_page_journals_dal($current_page, $page_size) {
    $skip = ($current_page - 1) * $page_size;
    $total_journals = getAllJournals();
    asort($total_journals);
    $list_current = array_slice($total_journals, $skip, $page_size);
    
    return $list_current;
}
